module.exports = app => {
  const express = require('express')
  const Specialty = require('../../models/Specialty')
  const Department = require(`../../models/Department`)
  const Teacher = require('../../models/Teacher')
  const User = require('../../models/User')
  const Student = require('../../models/Student')
  const Grade = require('../../models/Grade')
  const College = require('../../models/College')
  const Class = require('../../models/Class')
  const Course = require('../../models/Course')
  const AdminUser = require("../../models/AdminUser")
  const auth=require('../../middleware/auth')

  //这个是插入课表的接口，插入课表之后，会同时生成成绩
  const inerRouter = express.Router({
    mergeParams: true
  })
  inerRouter.post('/', async (req, res) => {
    let courseInsertList = [],
      errorMessage = ``,
      gradeList = [],
      preNid = ''
    let courseList = await Course.find({
      year: req.body[0].year,
      xq: req.body[0].xq
    });
    // await req.body.forEach(async (item, index) => {
    for (let i = 0; i < req.body.length; i++) {
      let item = req.body[i];
      if (i == 0 && courseList.length < 1) {
        preNid = `KC${item.year}${item.xq}00001`;
        item.nid = preNid;
      } else if (i == 0 && courseList.length >= 1) {
        preNid = courseList[courseList.length - 1].nid
        // FIXME:这里自动生成课表的切割法要换一下，因为年份变成了字符串
        let nextNid = preNid.slice(0, 2) + preNid.slice(2,11)+(parseInt(preNid.slice(11)) + 1)
        item.nid = nextNid;
        preNid = item.nid
      } else {
        let nextNid = preNid.slice(0, 2) + preNid.slice(2,11)+(parseInt(preNid.slice(11)) + 1)
        item.nid = nextNid;
        preNid = item.nid
      }

      let specialtyRes = await Specialty.findOne({
        nid: item.specialty
      });
      if (specialtyRes) {
        item.specialty = specialtyRes._id;
      }

      let TeahcerRes = await Teacher.findOne({
        nid: item.teacher
      });

      if (TeahcerRes) {
        item.teacher = TeahcerRes._id
      }

      let classRes = await Class.findOne({
        nid: item.classNid
      });
      if (classRes) {
        item.class = classRes._id;
      }


      if (!specialtyRes) {
        errorMessage += `第${i+1}行所填写的系别编号不存在，请修改后再填写;`
      }
      if (!TeahcerRes) {
        errorMessage += `第${i+1}行所填写的教师编号不存在，请修改后再填写;`
      }
      if (!classRes) {
        errorMessage += `第${i+1}行所填写的班级编号不存在，请修改后再填写;`
      }

      courseInsertList.push({
        nid: item.nid,
        coursename: item.coursename,
        teacher: item.teacher,
        coursenature: item.coursenature,
        specialty: item.specialty,
        time: item.time,
        year: item.year,
        xq: item.xq,
        address: item.address,
        week: item.week,
        zxf: item.zxf,
        zjd: item.zjd,
        class: item.class
      })
    }
    // });
    if (errorMessage.length > 0) {
      res.send({
        success: false,
        message: errorMessage
      })
    } else {
      Course.insertMany(courseInsertList, function (error, docs) {
        if (error) {
          return res.json({
            success: false,
            message: error
          })
        }
        docs.forEach(async (item, index) => {
          let studentList = await Student.find({
            class: item.class
          }, {
            'nid': 1
          });
          studentList.forEach((sitem, sindex) => {
            Grade.create({
              grade: 0,
              course: item._id,
              student: sitem._id,
              xf: 0,
              gpa: 0,
              isEdit: false
            })
          })
        })
        return res.json({
          success: true,
          message: "导入成功！"
        })

        // docs.forEach((item, index) {
        //     ShowCoursList[index].course = docs._id
        // })
      });
    }

  })
  app.use('/admin/api/inerstMore/Course',auth() ,inerRouter)
}